local HandbookHelper = {}

-- listmenu 结构
-- 1级目录
function HandbookHelper.getHandbookList()
    local thirdData = {}
    local thirdCaches = GD:queryDirectoryListConfs()
    for k ,v in pairs(thirdCaches) do
        local dirSub = tonumber(v.dirSub)
        if dirSub then
            local tempT
            if not thirdData[dirSub] then
                tempT = {}
                thirdData[dirSub] = tempT
            else
                tempT = thirdData[dirSub]
            end

            local dataId = tonumber(v.ID)
			if dataId == 52 then
				if UD.msdkData:isQQPlatform(true) then
					table.insert( tempT, {config = v} )
				end
			elseif dataId == 51 then
				if UD.msdkData:isWXPlatform(true) then
					table.insert( tempT, {config = v} )
				end
			else
                if UD:getLevel() >= tonumber( v.lv ) then -- 等级条件满足？
					table.insert( tempT, {config = v} )
				end
			end
        end
    end

    local subKeys = GD:getCachedValues("directory_subkey")
    local tempDatas = {}
    local maxIndex = 0
    for k, v in pairs(subKeys) do
        local keyIndex = tonumber(v.subKey)
        if keyIndex > maxIndex then
            maxIndex = keyIndex
        end
        local tempT = tempDatas[keyIndex]
        if not tempT then
            tempT = {}
            tempDatas[keyIndex] = tempT
        end
        table.insert( tempT, v)
    end

    local retDatas = {}
    -- 按照索引顺序加入
    for i = 0, maxIndex do
        local d = tempDatas[i]
        if d then
            local t = {name = L("directory_"..i), children = {},key = i}
            for k, v in pairs(d) do
                local dirSub = tonumber(v.ID)
                local thirdData = thirdData[dirSub]
                if thirdData and #thirdData > 0 then
                    -- table.sort(thirdData, sortThirdFunc)
                    table.insert(t.children, {name = L(v.name), subKey = dirSub, config = v, contents = thirdData})
                end
            end
           
            if #t.children == 1 then
                t.config = t.children[1].config
                t.contents = t.children[1].contents
                t.subKey =  t.children[1].subKey
                t.children  = nil
                table.insert(retDatas, t)
            elseif #t.children > 1 then
                table.insert(retDatas, t)
			elseif #t.children == 0 then
				t.subKey =  tonumber(d[1].ID)
				t.children  = nil
				table.insert(retDatas, t)
            end
        end
    end
    return retDatas
end

function HandbookHelper.getItemProgress(aid)
	local BoxTypeByIndex = {[1] = Enums.BoxType.dungeon, [2] = Enums.BoxType.pvp, [3] = Enums.BoxType.eventBox}
	if aid == 101 then --每日任务数量
		local m = app:getInst("DailyModel")
		local t = m:getTotalDailyTaskNumber()
		local c = m:getFinishedDailyTaskNumber()

		if t - c <= 0 then
			return -1
		else
			return t - c 
		end
	elseif aid == 102 then --日常宝箱
		local t = 0
		for i = 1, 3 do
			t = t + checknumber(UD:getLuckBoxRestTimesByType(BoxTypeByIndex[i]))
		end
		if t <= 0 then
			return -1
		end
		return t
	elseif aid == 103 then --日常祝福
		local m = app:getInst("WorldMapModel")
		local r = m:getBlessTimes()
		if r <= 0 then
			return -1
		else
			return r 
		end
	elseif aid == 104 then --地城宝箱
		local r = checknumber(UD:getLuckBoxRestTimesByType(BoxTypeByIndex[1]))
		if r <= 0 then
			return -1
		else
			return r 
		end
	elseif aid == 105 then --竞技宝箱
		local r = checknumber(UD:getLuckBoxRestTimesByType(BoxTypeByIndex[2]))
		if r <= 0 then
			return -1
		else
			return r 
		end
	elseif aid == 106 then --活动宝箱
		local r = checknumber(UD:getLuckBoxRestTimesByType(BoxTypeByIndex[3]))
		if r <= 0 then
			return -1
		else
			return r 
		end
	elseif aid == 107 then --锦标赛
		local info = app:sendMsg("PvpModel", "getPvpInfo")
		if info and info.seasonInfo then
			if info.seasonInfo.lv == 1 then
				return -1
			else
				return info.seasonInfo.lv
			end
		end
	elseif aid == 108 then --疲劳度
		local expBattle = UD:getExpDaily()
		local level = UD:getExpRewardLevel()
		local cfg = GD:getBattleExpLimit(level)
		local expLimit = cfg.stageA + cfg.stageB + cfg.stageC
		local r = expBattle*100 /expLimit
		if r >= 100 then
			return -1
		else
			return math.floor(r)
		end
	elseif aid == 109 then --每日任务宝箱数量
		local m = app:getInst("DailyModel")
		local dailyTaskInfo = TaskManager:getDailyTaskInfo()
		local progress = dailyTaskInfo:getDailyTaskProgress()
	
		if progress and #progress >= 3 then
			local r = m:getDailyTaskRewardProgress(progress[1].NeedProgress).NowProgress+
			m:getDailyTaskRewardProgress(progress[2].NeedProgress).NowProgress+
			m:getDailyTaskRewardProgress(progress[3].NeedProgress).NowProgress

			if r <= 0 then
				return -1
			else
				return r 
			end
        end
	elseif aid == 110 then --地下城 经验卡
		return UD:getUserExpCardAllCount()
	elseif aid ==111 then -- 狮心号角
		local targetConf = GD:queryTeamTargetById( 301 )
        -- 存在刷新类型 并且 存在奖励次数
        if 0 ~= tonumber( targetConf.refreshType ) and 0 ~= tonumber( targetConf.num ) then
            local teamTarget = app:sendMsg("TeamModel", "getTeamTargetById", tonumber(targetConf.ID))
            if teamTarget then
                return teamTarget:getRestRewardNum()
            end
		end
		return -1
	elseif aid == 112 then --赏金任务
		local bountyTaskInfo = TaskManager:getBountyTaskInfo()
		local restTimes = -1
		if bountyTaskInfo then
			local finishedNum = bountyTaskInfo:getFinishedNum()
			local maxNum = bountyTaskInfo:getMaxNum()
			if finishedNum then
				restTimes = maxNum - finishedNum
			end
		end
		return restTimes
    end
end
return HandbookHelper